package com.mygdx.game;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapLayers;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.MapObjects;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;

/**
 * Created by 80002023 on 2016/8/3.
 */
public class SuperMarioCollisionTest extends Game {

    private OrthographicCamera camera;
    private SpriteBatch batch;
    private Texture texture;
    TiledMap map;
    OrthogonalTiledMapRenderer render;
    TextureRegion mario;
    private static int[][] barriers;
    float x;
    float y;

    public static final int MAP_TILE_WIDTH = 48;
    public static final int MAP_TILE_HEIGHT = 48;

    @Override
    public void create() {
        batch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("data/Mario.png"));
        mario = new TextureRegion(texture, 0, 0, 64, 64);

        map = new TmxMapLoader().load("data/1.tmx");
        barriers = new int[10][10];
        render = new OrthogonalTiledMapRenderer(map);
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 480, 336);
        setMario();
    }

    public void setMario() {

        MapLayers layers = map.getLayers();

        for (MapLayer layer : layers) {
            if (layer.getName().equals("actor")) {

                MapObjects objs = layer.getObjects();
                for (MapObject obj : objs) {
                    RectangleMapObject ro = (RectangleMapObject) obj;
                    if (ro.getName().equals("mario")) {
                        x = ro.getRectangle().x;
                        y = ro.getRectangle().y;
                    }


                }

            } else if (layer.getName().equals("barriers")) {
                if (layer instanceof TiledMapTileLayer) {
                    TiledMapTileLayer tileLayer = (TiledMapTileLayer) layer;
                    //j为高（行） i为宽（列）
                    for (int j = 10; j > 0; j--) {
                        for (int i = 0; i < 10; i++) {
                            //getCell(列，行) 纵坐标翻转
                            TiledMapTileLayer.Cell cell = tileLayer.getCell(i, j);
                            if (cell != null) {
                                barriers[j][i] = 1;
                            }
                        }
                    }

                }
            }
        }

    }

    public void update() {
        if(passEnable(x, y)) {
            y -=1.5f;
        }
    }

    public static boolean passEnable(float x,float y){
		//System.out.println("检测位置："+(int)y +"," +(int)x);
        return barriers[(int)y/MAP_TILE_HEIGHT][(int)x/MAP_TILE_WIDTH]==0;
    }

    @Override
    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        camera.update();
        render.setView(camera);
        render.render();

        update();

        batch.begin();
        batch.draw(mario, x, y);
        batch.end();

    }
}
